﻿//704.二分查找
class Solution {
public:
    int search(vector<int>& nums, int target) {
        int left = 0, right = nums.size() - 1;
        while (left <= right)
        {
            int mid = (left + right) / 2;

            if (nums[mid] < target)
            {
                left = mid + 1;
            }
            else if (nums[mid] > target)
            {
                right = mid - 1;
            }
            else
            {
                return mid;
            }
        }
        return -1;
    }
};

//34.在排序数组中查找元素的第一个和最后一个位置
class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        if (nums.size() == 0)
        {
            return { -1, -1 };
        }

        vector<int> ans;

        int left = 0, right = nums.size() - 1;
        while (left < right)
        {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target)
            {
                left = mid + 1;
            }
            else
            {
                right = mid;
            }
        }

        if (nums[left] == target)
        {
            ans.push_back(left);
        }
        else
        {
            ans.push_back(-1);
        }

        left = 0, right = nums.size() - 1;
        while (left < right)
        {
            int mid = left + (right - left + 1) / 2;
            if (nums[mid] > target)
            {
                right = mid - 1;
            }
            else
            {
                left = mid;
            }
        }

        if (nums[right] == target)
        {
            ans.push_back(right);
        }
        else
        {
            ans.push_back(-1);
        }

        return ans;
    }
};

//面试题 08.01.三步问题
class Solution {
public:
    int waysToStep(int n) {
        if (n == 1) return 1;
        if (n == 2) return 2;

        int MOD = 1e9 + 7;
        int a = 1, b = 1, c = 2, d = 0;
        for (int i = 3; i <= n; i++)
        {
            d = ((a + b) % MOD + c) % MOD;
            a = b;
            b = c;
            c = d;
        }

        return d;
    }
};